use ane_system::dao::{LoginLogVo, SysLog};
use axum::{
    extract::{Path, Query},
    response::IntoResponse,
    Extension,
};
use common::page::{PageInfo, PageParams};
use serde::Deserialize;

use crate::{error::AppResult, extract::Json, state::ShareState};

#[derive(Deserialize)]
pub struct ListQuery {
    username: Option<String>,
    result: Option<i8>,
}

pub async fn login_log_list(
    Extension(state): Extension<ShareState>,
    Query(query): Query<ListQuery>,
    Query(page_params): Query<PageParams>,
) -> AppResult<impl IntoResponse> {
    SysLog::list_login::<LoginLogVo>(
        state.db(),
        page_params.page - 1,
        page_params.page_size,
        query.username,
        query.result,
    )
    .await
    .map(|(total, datas)| Json(PageInfo::new(datas, &page_params, total)))
    .map_err(Into::into)
}

pub async fn login_log_delete(
    Extension(state): Extension<ShareState>,
    Path(id): Path<i32>,
) -> AppResult<impl IntoResponse> {
    SysLog::del_login_log(state.db(), id)
        .await
        .map(Json)
        .map_err(Into::into)
}

pub async fn operate_log_list(
    Extension(state): Extension<ShareState>,
    Query(query): Query<ListQuery>,
    Query(page_params): Query<PageParams>,
) -> AppResult<impl IntoResponse> {
    SysLog::list_operate(
        state.db(),
        page_params.page - 1,
        page_params.page_size,
        query.result,
    )
    .await
    .map(|(total, datas)| Json(PageInfo::new(datas, &page_params, total)))
    .map_err(Into::into)
}

pub async fn operate_log_delete(
    Extension(state): Extension<ShareState>,
    Path(id): Path<i32>,
) -> AppResult<impl IntoResponse> {
    SysLog::del_operate_log(state.db(), id)
        .await
        .map(Json)
        .map_err(Into::into)
}
